package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Inventory;
import com.weipai.springboot.model.InventoryIn;
import com.weipai.springboot.model.InventoryOut;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.InventoryDao;
import com.weipai.springboot.module.dao.InventoryDetailDao;
import com.weipai.springboot.module.dao.OrderDao;
import com.weipai.springboot.module.service.StorageDetailService;
import com.weipai.springboot.module.util.DateUtils;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author songmengqiang
 * @create 2018-08-24 11:25
 **/
@Service
public class StorageDetailServiceImpl implements StorageDetailService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private InventoryDetailDao inventoryDetailDao;
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    SessionUserUtil sessionUserUtil;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response outStorage(String storageId, String shopId, String skuNumber, Integer outNums, String remark) {
        Response response = new Response();
//        String adminId = sessionUserUtil.getAdminAttr().getAdminId();
        List<InventoryOut> outList = new ArrayList<>();
        //减掉商品的库存
        Map<String, Object> inventoryMap = inventoryDao.getStorageGoodsNum(storageId, skuNumber);
        if (null != inventoryMap) {
            int goodsNumber = Integer.parseInt(inventoryMap.get("goodsNumber").toString());
            if (goodsNumber >= outNums) {
                //记录商品出库明细
                InventoryOut out = new InventoryOut();
                out.setStorageId(storageId);
                out.setShopId(shopId);
                out.setSkuNumber(skuNumber);
                out.setType(1);
                out.setOutNums(outNums);
                out.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
//                out.setAdminId(adminId);
                out.setRemark(remark);
                outList.add(out);
                Inventory inventory = inventoryDao.selectByPrimaryKey((Long) (inventoryMap.get("inventoryId")));
                inventory.setGoodsNumber(inventory.getGoodsNumber() - outNums);
                inventoryDao.updateByPrimaryKeySelective(inventory);
                //批量保存出库明细
                if (!outList.isEmpty()) {
                    orderDao.saveInventoryOutList(outList);
                }
                response.setCode(RetCode.SUCCESS);
                return response;
            } else {
                response.setCode(RetCode.STORAGE_GOODSNOTENOUGH);
                return response;
            }
        } else {
            response.setCode(RetCode.STORAGE_GOODSNUMBERNULL);
            return response;
        }
    }


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response inStorage(InventoryIn inventoryIn) {
        Response response = new Response();
        inventoryIn.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        inventoryDetailDao.inStorage(inventoryIn);

        Map<String, Object> map = inventoryDao.getStorageGoodsNum(inventoryIn.getStorageId(), inventoryIn.getSkuNumber());
        if (null != map) {
            int allNum = Integer.parseInt(map.get("goodsNumber").toString()) + inventoryIn.getInNums();
            inventoryDao.updateGoodsNumber(map.get("inventoryId").toString(), allNum);
        } else {
            Inventory inventory = new Inventory();
            inventory.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
            inventory.setGoodsNumber(inventoryIn.getInNums());
            inventory.setShopId(inventoryIn.getShopId());
            inventory.setSkuNumber(inventoryIn.getSkuNumber());
            inventory.setStorageId(inventoryIn.getStorageId());
            inventoryDao.insert(inventory);
        }

        response.setCode(RetCode.SUCCESS);
        return response;
    }


    //入库明细
    @Override
    public Response listInDetail(String categoryId, Integer page, Integer size, String draw, String search) {
        Response response = new Response();
        Integer totalNum = inventoryDetailDao.countIn(categoryId, search);
        List<Map<String, Object>> list = inventoryDetailDao.listInDetail(categoryId, (page - 1) * size, size, search);
        PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, totalNum, draw);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    //出库明细
    @Override
    public Response listOutDetail(String categoryId, Integer page, Integer size, String draw, String search) {
        Response response = new Response();
        Integer totalNum = inventoryDetailDao.countOut(categoryId, search);
        List<Map<String, Object>> list = inventoryDetailDao.listOutDetail(categoryId, (page - 1) * size, size, search);
        PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, totalNum, draw);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }


}
